//
//  ByDocumentRedsCell.m
//  caipiao_ios
//
//  Created by mc on 2018/8/16.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByDocumentRedsCell.h"

#import "ByRedEmptyView.h"

@implementation ByDocumentRedsCell

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByDocumentRedsCell";
    ByDocumentRedsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.contentView.backgroundColor = BY_BACKGROUND_COLOR;
    
    }
    return self;
}


- (void)initViews{
    
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UIView *backgroundView = [[UIView alloc] init];
    [self.contentView addSubview:backgroundView];
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(5);
        make.right.equalTo(self.contentView).offset(-5);
        make.bottom.equalTo(self.contentView).offset(-9);
        make.top.equalTo(self.contentView).offset(5);
    }];
    backgroundView.backgroundColor = [UIColor whiteColor];
    backgroundView.layer.cornerRadius = 5;
    
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 10, 39)];
    [backgroundView addSubview:header];
    
    UILabel *line = [[UILabel alloc] init];
    [header addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(header).offset(11);
        make.centerY.equalTo(header).offset(0);
        make.size.mas_equalTo(CGSizeMake(4, 17));
    }];
    line.backgroundColor = BY_THEME_COLOR;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [header addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(header).offset(0);
        make.left.equalTo(line.mas_right).offset(5);
        make.height.mas_equalTo(@20);
        make.width.mas_equalTo(@40);
    }];
    
    titleLabel.font = B_FONT(17);
    titleLabel.textColor = BY_THEME_COLOR;
    titleLabel.text = @"红人";
    
    
    UIImageView *redView = [[UIImageView alloc] init];
    [header addSubview:redView];
    [redView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right).offset(0);
        make.centerY.equalTo(header).offset(0);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    redView.image = [UIImage imageNamed:@"hongren红人"];
    
    
    UIButton *moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [header addSubview:moreBtn];
    [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(header).offset(0);
        make.right.equalTo(header).offset(-11);
        make.size.mas_equalTo(CGSizeMake(50, 15));
    }];
    moreBtn.titleLabel.font = FONT(13);
    [moreBtn setTitle:@"更多>>" forState:UIControlStateNormal];
    [moreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [moreBtn addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat width = (SCREEN_WIDTH - 10)/ 4.0;
    CGFloat height = 170 - 15 - 40;
    NSInteger count = _dataArray.count;
    if (count > 4) {
        count = 4;
    }
    for (int i = 0; i < count; i++) {
        ByNetModel *netModel = [_dataArray objectAtIndex:i];
        
        UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(width * i, 40 , width, height)];
        [backgroundView addSubview:control];
        control.userInteractionEnabled = YES;
        
        UIImageView *iconView = [[UIImageView alloc] init];
        [control addSubview:iconView];
        [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(control).offset((height - width * 0.5 - 26) / 2.0);
            make.centerX.equalTo(control).offset(0);
            make.size.mas_equalTo(CGSizeMake(width *0.5, width * 0.5));
        }];
        iconView.layer.cornerRadius = width * 0.25;
        iconView.layer.masksToBounds = YES;
        [iconView sd_setImageWithURL:[NSURL URLWithString:netModel.head_pic] placeholderImage:HEAD_ICOM_IMAGE];
        iconView.userInteractionEnabled = YES;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [control addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(iconView.mas_bottom).offset(11);
            make.left.equalTo(control).offset(2);
            make.right.equalTo(control).offset(-2);
            make.height.mas_equalTo(@15);
        }];
        titleLabel.font = FONT(14);
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = BY_TEXT_COLOR;
        titleLabel.text = netModel.nickname;
        
        control.tag = 100 + i;
        [control addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        
        iconView.tag = 200 + i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapViewAction:)];
        [iconView addGestureRecognizer:tap];
    }
    
    if (count == 0) {
        ByRedEmptyView *emptyView = [[ByRedEmptyView alloc] init];
        [backgroundView addSubview:emptyView];
        [emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backgroundView).offset(0);
            make.right.equalTo(backgroundView).offset(0);
            make.bottom.equalTo(backgroundView).offset(0);
            make.top.equalTo(backgroundView).offset(40);
        }];
    }
    
    
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self initViews];
}


- (void)tapViewAction:(UITapGestureRecognizer *)tap{
    
    if (self.selectBlock) {
        self.selectBlock(tap.view.tag - 200);
    }
}

- (void)moreAction:(UIButton *)btn{
    if (self.moreBlock) {
        self.moreBlock();
    }
    
}

- (void)tapAction:(UIControl *)control{
    
    if (self.selectBlock) {
        self.selectBlock(control.tag - 100);
    }
    
}

@end
